package Reflect;

import java.lang.reflect.Constructor;

/**
 * Created by 86185 on 2019/7/21.
 */
public class Hello {
    public static void main(String[] args) {
        Demo demo = new Demo();
        /*System.out.println(demo.getClass().getName());
       Class<?> demo1=null;
        Class<?> demo2=null;
        Class<?> demo3=null;
        try{
            demo1=Class.forName("Reflect.Demo");
        }catch (Exception e){
            e.printStackTrace();
        }

        demo2=new Demo().getClass();
        demo3=Demo.class;
        System.out.println("类名称   "+demo1.getName());
        System.out.println("类名称   "+demo2.getName());
        System.out.println("类名称   "+demo3.getName());

i+1+i=0
        Class<?> demo = null;
        try{
            demo=Class.forName("Reflect.Person");
        } catch (Exception e) {
            e.printStackTrace();
        }
            Person per=null;
        try{
            per=(Person)demo.newInstance();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        per.setName("Rollen");
        per.setAge(20);
        System.out.println(per);

       Class<?> demo = null;
        try {
            demo=Class.forName("Reflect.Person");
        }catch (Exception e){
            e.printStackTrace();
        }
        Person per1=null;
        Person per2=null;
        Person per3=null;
        Person per4=null;
        //取得全部的构造函数
        Constructor<?> cons[]=demo.getConstructors();
        try{
            per1=(Person)cons[0].newInstance();
            per2=(Person)cons[1].newInstance("Rollen");
            per3=(Person)cons[2].newInstance(20);
            per4=(Person)cons[3].newInstance("Rollen",20);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println(per1);
        System.out.println(per2);
        System.out.println(per3);
        System.out.println(per4);

       Class<?> demo=null;
       try{
           demo=Class.forName("Reflect.Person");
       }catch (Exception e){
           e.printStackTrace();
       }
        //保存所有的接口
        Class<?> intes[]=demo.getInterfaces();
        for (int i = 0; i <intes.length ; i++) {
            System.out.println("实现的接口   "+intes[i].getName());
        }

       Class<?> demo=null;
       try{
           demo=Class.forName("Reflect.Person");
       }catch (Exception e){
           e.printStackTrace();
       }
        //取得父类
        Class<?> temp=demo.getSuperclass();
        System.out.println("继承的父类为：   "+temp.getName());
       Class<?> demo=null;
       try{
           demo=Class.forName("Reflect.Person");
       }catch (Exception e){
           e.printStackTrace();
       }
        Constructor<?>cons[]=demo.getConstructors();
        for (int i = 0; i <cons.length; i++) {
            System.out.println("构造方法：  "+cons[i]);
        }
    }
    constructor<?>[] cons=demo.getConstrutors();
    for(int i=0;i<cons.length;i++){
        class p[]=cons[i].getParameterTypes();
        System.out.println("构造函数:");
        int mo=cons[i].getModifiers();
        System.out.println(Modifiers.toString(mo)+" ");
        System.out.println(cons[i].getName()+"(");
        for (int j=0;j<p.length;j++){
            System.out.println(p[j].getName()+"arg"+j);
            if (j<p.length-1) System.out.println(",");
        }
        System.out.println("){}");
    }
Field[] fields=demo.getDeclaredFields();
    for(int i=0;i<fields.legth;i++){
        int mo=fields[i].getModifiers();
        String mod=Modifier.toString(mo);
        String fname=fields[i].getname();
        Class ctype=fields[i].gettype();
    }newince
        Method method=demo.getMethod("sathchina") ; char
        meth.invoke(demo.newInstance());//反转 invoke
        method = demo.getMethod("sayHello", String.class, int.class);
        method.invoke(demo.newInstance(), "欧克", 18)//x`反转
    }*/
        //调用getter setter
        Object object = demo.newInstance();
        Method setter = object.getClass().getMethod("setName", String.class);
        setter.invoke(object, "是金子");

        Method getter = object.getClass().getMethod("getName");
        Object name = getter.invoke(object);
        System.out.println(name);
    }
int[] temp={1,2,3,4,5};
    int num=10;
    demo=temp.getClass(),getComponentType();//获取类型
    Object object=Array.newInsteance(demo,num);//创建新按钮
    int new_len=Math.min(temp.length,num);//比较大小
    System.arraycopy(temp,0,object.0,new_len);
    int[] new_arr=(int [])objects;
    for(int i:new_arr){
        System.out.print(i+" ");
    }
    }
